#!/bin/bash
#
APP_NAME=sbt-study
APP_VERSION=1.0-SNAPSHOT
JAR_NAME=${APP_NAME}-${APP_VERSION}.jar

DATE_VERSION=$(date +%Y%m%d%H%M%S)


if [ -z "${PROJECT_HOME}" ]; then
  export PROJECT_HOME="$(cd "`dirname "$0"`"/.; pwd)"
fi

JAVA_OPTS="-Dfile.encoding=UTF-8 -Dlog4j.configurationFile=file://${PROJECT_HOME}/conf/log4j2.xml "

JARS=$(echo ${PROJECT_HOME}/libs/*.jar | tr ' ' ':'):$(echo ${PROJECT_HOME}/jars/*.jar | tr ' ' ':'):$(echo ${PROJECT_HOME}/*/target/scala-2.11/*.jar | tr ' ' ':')


function package() {
    cd ${PROJECT_HOME}
    echo "Package App $APP_NAME-$APP_VERSION"
    mvn clean package -DskipTests "$@"
}

function clean() {
    cd ${PROJECT_HOME}
    echo "Clean App $APP_NAME-$APP_VERSION"
    mvn clean "$@"
}

function echo_build_properties() {
  echo version=$APP_VERSION
  echo user=$USER
  echo revision=$(git rev-parse HEAD)
  echo branch=$(git rev-parse --abbrev-ref HEAD)
  echo date=$(date +"%Y/%m/%d %H:%M:%S")
  echo url=$(git config --get remote.origin.url)
}

function build_info() {
    echo_build_properties $2 > ${PROJECT_HOME}/INFO
}

function usage() {
cat << EOF
  Usage: ./bootstrap.sh package
         ./bootstrap.sh clean
         ./bootstrap.sh start ...
         ./bootstrap.sh run ...
         ./bootstrap.sh stop
         ./bootstrap.sh status
         ./bootstrap.sh help
EOF
}

function run() {
   JARS=$(echo ${PROJECT_HOME}/*/target/scala-2.11/*.jar | tr ' ' ':'):$JARS
   java $JAVA_OPTS -cp $JARS "$@"
}
case "$1:$2:$3" in
    package:*)
        package "${@:2}"
        ;;
    clean:*)
        clean "${@:2}"
        ;;
    status:*:*)
        status
        ;;
    start:*:*)
        run "${@:2}"
        ;;
    run:*:*)
        run "${@:2}"
        ;;
    build_info:*:*|bi:*:*)
        build_info
        ;;
    h|help)
        usage
        ;;
    *)
        usage
        exit 0
esac
